package tq.com.tqteacher.optimize.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import tq.com.systools.system.AlertDialogHelper;
import tq.com.tqcom.base.BaseFragment;
import tq.com.tqcom.base.BaseRecyclerFragment;
import tq.com.tqcom.base.BaseTitleRecycleFragment;
import tq.com.tqcom.net.INetCallback;
import tq.com.tqcom.net.TQNetResult;
import tq.com.tqcom.view.TQHeader5View;
import tq.com.tqteacher.R;
import tq.com.tqteacher.fragment.TQHomePublishFragment;
import tq.com.tqteacher.inter.INewPulibshListener;
import tq.com.tqteacher.model.TQCommentOverInfoModel;
import tq.com.tqteacher.model.TQLogin;
import tq.com.tqteacher.model.TQLookerOverInfo;
import tq.com.tqteacher.model.TQNewPublish;
import tq.com.tqteacher.model.TQPublishType;
import tq.com.tqteacher.net.TQNetInterface;
import tq.com.tqteacher.optimize.adapt.AdapterOptimize;

public class FragmentOptimize extends BaseTitleRecycleFragment {
    private AdapterOptimize mAdapterOptimize;
    private INewPulibshListener mNewPulibshListener;
    private int page = 1;

    static public FragmentOptimize newInstanc(INewPulibshListener pPulibshListener){
        FragmentOptimize lFragmentOptimize = new FragmentOptimize();
        lFragmentOptimize.mNewPulibshListener = pPulibshListener;
        return lFragmentOptimize;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initHeader(TQHeader5View pHeader5View) {
        super.initHeader(pHeader5View);
        pHeader5View.getBackIcon().setVisibility(View.INVISIBLE);
        pHeader5View.getTitleView().setText("优化");
    }

    protected void initRecycleView(RecyclerView pRecyclerView) {
        if(mAdapterOptimize == null){
            mAdapterOptimize = new AdapterOptimize(this.getContext());
        }
        pRecyclerView.setAdapter(mAdapterOptimize);
        pRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
    }

    @Override
    protected void initRefresh(SmartRefreshLayout pRefreshLayout) {
        pRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                loadData();
            }
        });

        pRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                loadData();
            }
        });

        pRefreshLayout.autoRefresh();
    }

    private void loadData(){
        int cls = TQLogin.getCurrent().getUserInfo().getClzid();
        int subject = TQLogin.getCurrent().getUserInfo().getSubject();

        TQNetInterface.getOptimizeList(cls, subject, page, new INetCallback() {
            @Override
            public void result(TQNetResult pNetResult) {
                boolean isRefresh = false;
                if(mRefreshLayout.getState() == RefreshState.Loading){
                    mRefreshLayout.finishLoadMore();
                }else{
                    isRefresh = true;
                    mRefreshLayout.finishRefresh();
                }

                if(!pNetResult.isSuccess() || pNetResult.getData() == null){
                    AlertDialogHelper.show(FragmentOptimize.this.getContext(), pNetResult.getErrMsg());
                    return;
                }

                ArrayList<TQNewPublish> lNewPublishes = new ArrayList<>();
                JSONArray material = pNetResult.getData().optJSONArray("material");
                for (int i = 0; i < material.length(); i++){
                    try {
                        JSONObject lObject = material.getJSONObject(i);
                        JSONObject lPublish = lObject.optJSONObject("material");
                        int type = lPublish.optInt("category");

                        if(!TQPublishType.isValidType(type)){
                            continue;
                        }

                        TQNewPublish lPublish1 = TQPublishType.getNewPublishByType(type);
                        lPublish1.fromJson(lPublish);

                        TQCommentOverInfoModel lOverInfoModel = new TQCommentOverInfoModel(lObject);
                        lPublish1.setCommentOverInfoModel(lOverInfoModel);

                        TQLookerOverInfo lLookerOverInfo = new TQLookerOverInfo(lObject);
                        lPublish1.setLookerOverInfo(lLookerOverInfo);

                        lNewPublishes.add(lPublish1);
                    }catch (JSONException pE){
                        pE.printStackTrace();
                    }
                }

                if(isRefresh){
                    mAdapterOptimize.clear();
                }
                mAdapterOptimize.appendData(lNewPublishes);

                if(material.length() == 0 && !isRefresh){
                    mRefreshLayout.finishLoadMoreWithNoMoreData();
                }
            }
        });
    }

}
